/**
 * Created by a1 on 16/9/20.
 */

var kID = "id"
var kTitle = "title"
var kContent = "content"
var kDate = "date"
var kImageUrl = "imgUrl"

function isEmpty(s) {
    return s == null || s == "" || s == undefined
}

//绑定下拉刷新
function bindRefresh(node, f) {
    node.on('refresh', f)
}

//绑定上拉加载事件
function bindLoadMore(node, f) {
    node.on('infinite', f)
}

//添加上拉加载控件
function addLoadMore(node) {

    if(node.hasClass("infinite-scroll")) {
        return
    }
    node.addClass("infinite-scroll")
    var footer = $("<div class='infinite-scroll-preloader'><div class='preloader'></div></div>")
    node.append(footer)
    $.attachInfiniteScroll(node)
}

//删除上拉加载控件
function removeLoadMore(node) {
    if(node.hasClass("infinite-scroll")) {
        node.removeClass("infinite-scroll")
        var footer = node.find(".infinite-scroll-preloader")
        footer.remove()
        $.detachInfiniteScroll(node)
    }
}

//type表示要添加的item的类型,在css中读取该值以展示不同的样式
//type取值含义:
// 1或空 -> 首页样式
// 2 -> 详情相关推荐样式
// 3 -> 单行样式
// 4 -> 搜索结果样式,即不显示content

function addListItem(parent, data, type) {
    //console.log("data = " + JSON.stringify(data))
    //console.log("add list item = " + parent)
    //console.log("id = "+data[kID])
    var item = $("<a data-no-cache='true'></a>")
    item.attr("href", "detail.html?id=" + data[kID])
    //item.attr("href", "test"+ id +".html?id=" + data[kID])
    var content = $("<div class='dm_list_item' type='"+ type +"'></div>")
    var containerLeft = $("<div class='dm_list_item_container_left' type='"+ type +"'></div>")
    var labelTitle = $("<div class='dm_list_item_text_title' type='"+ type +"'>"+data[kTitle]+"</div>")
    containerLeft.append(labelTitle)
    if(type != 4){
        var labelContent = $("<div class='dm_list_item_text_content' type='"+ type +"'>"+data[kContent]+"</div>")
        containerLeft.append(labelContent)
    }

    var labelDate = $("<div class='dm_list_item_text_date' type='"+ type +"'>"+data[kDate]+"</div>")
    containerLeft.append(labelDate)
    content.append(containerLeft)

    var imageUrl = data[kImageUrl]
    var hasImage = ($.trim(imageUrl) != "")
    if(hasImage) {
        var imageView = $("<img class='dm_list_item_image' type='"+ type +"' src='"+imageUrl+"'/>")
        content.append(imageView)
        //重置文本与item的size
        containerLeft.addClass("dm_shrink")
        content.addClass("dm_shrink")
    }
    var divider = "<div class='dm_divider' type='"+ type +"'></div>"
    item.append(content)
    item.append(divider)
    parent.append(item)
    return labelDate
}

function toDetail(data) {
    console.log("data = " + JSON.stringify(data))
    detailData = data
    //$.router.load("#dm_page_detail", true)
    $.router.load("detail.html", true)
}

function toList(type, data) {
    listType = type
    listData = data
    //$.router.load("#dm_page_list", true)
    $.router.load("list.html", true)
}

$.fn.dm_scrollTo = function(options){
    var defaults = {
        toT : 0,    //滚动目标位置
        durTime : 500,  //过渡动画时间
        delay : 30,     //定时器时间
        callback:null   //回调函数
    };
    var opts = $.extend(defaults,options),
        timer = null,
        _this = this,
        curTop = _this.scrollTop(),//滚动条当前的位置
        subTop = opts.toT - curTop,    //滚动条目标位置和当前位置的差值
        index = 0,
        dur = Math.round(opts.durTime / opts.delay),
        smoothScroll = function(t){
            index++;
            var per = Math.round(subTop/dur);
            if(index >= dur){
                _this.scrollTop(t);
                window.clearInterval(timer);
                if(opts.callback && typeof opts.callback == 'function'){
                    opts.callback();
                }
                return;
            }else{
                _this.scrollTop(curTop + index*per);
            }
        };
    timer = window.setInterval(function(){
        smoothScroll(opts.toT);
    }, opts.delay);
    return _this;
};